<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\menu\util\row_events;

/**
 * AJAX事件
 * @author cleverstone
 * @since ym2.0
 */
class Ajax extends RowEventType
{
    /**
     * @var array 选项
     */
    public $_options = [
        'type' => 'ajax',//支持modal/ajax
        'title' => '',
        'icon' => '',
        'route' => '',
        'ignoreRBAC' => false,//路由是否忽略RBAC校验
        'method' => 'GET',
        'questionTitle' => '', // 询问标题
        'questionDescription' => '确定要执行该操作么?', // 询问描述
    ];

    /**
     * @param string $title 标题
     * ```php
     * // 文本标题
     * $this->title('编辑');
     *
     * ```
     * @return $this
     */
    public function title($title)
    {
        $this->_options['title'] = $title;
        return $this;
    }

    /**
     * @param string $icon 图标
     * 支持的图标有:
     * - bootstrap v4图标
     * - fontawesome v5
     * - layui 2.8图标
     *
     * @return $this
     */
    public function icon($icon)
    {
        $this->_options['icon'] = $icon;
        return $this;
    }

    /**
     * @param string|array $route 路由或外链
     * ```php
     * 可以直接指定路由
     * $this->route('user/edit');
     *
     * 也可以指定外部链接
     * $this->route('https://ym2.cleverstone.top');
     *
     * ```
     * @return $this
     */
    public function route($route, $ignoreRBAC = false)
    {
        $this->_options['route'] = $route;
        $this->_options['ignoreRBAC'] = (boolean)$ignoreRBAC;
        return $this;
    }

    /**
     * @param string $method HTTP请求动作, 目前支持GET(助手默认), POST
     * @return $this
     */
    public function method($method = 'POST')
    {
        $this->_options['method'] = strtoupper($method);
        return $this;
    }

    /**
     * @param string $questionTitle 询问标题
     * @return $this
     */
    public function questionTitle($questionTitle)
    {
        $this->_options['questionTitle'] = $questionTitle;
        return $this;
    }

    /**
     * @param string $questionDescription 询问描述
     * @return $this
     */
    public function questionDescription($questionDescription)
    {
        $this->_options['questionDescription'] = $questionDescription;
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        if (
            $this->_options['route']
            && !$this->_options['ignoreRBAC']
            && !$this->checkAccess($this->_options['route'])
        ) {
            return [];
        }

        return $this->_options;
    }
}